//
//  UIView+XibView.m
//  联付商圈
//
//  Created by 韩佳睿 on 2017/7/18.
//  Copyright © 2017年 lianfu. All rights reserved.
//

#import "UIView+XibView.h"

@implementation UIView (XibView)
+(id)viewFromXib
{
    Class viewClass = [self class];
    
    NSString *viewClassName = [NSString stringWithFormat:@"%@", viewClass];
    
    if (IS_IPHONE_5)
    {
        NSString *originViewClassName = viewClassName;
        
        viewClassName = [viewClassName stringByAppendingString:@"_iPhone5"];
        
        NSString *nibPath = [[NSBundle mainBundle] pathForResource:viewClassName ofType:@"nib"];
        
        if (!nibPath)
        {
            viewClassName = originViewClassName;
        }
    }
    
    UINib *nib = [UINib nibWithNibName:viewClassName bundle:nil];
    return [nib instantiateWithOwner:self options:nil][0];
}

- (void)addSubviewWithFullConstraint:(UIView *)view {
    [self addSubview:view];
    self.translatesAutoresizingMaskIntoConstraints = NO;
    view.translatesAutoresizingMaskIntoConstraints = NO;
    
    NSLayoutConstraint *leadingConstraint = nil;
    NSLayoutConstraint *trailingConstraint = nil;
    NSLayoutConstraint *topConstraint = nil;
    NSLayoutConstraint *bottomConstraint = nil;
    
    leadingConstraint   = [NSLayoutConstraint constraintWithItem:view
                                                       attribute:NSLayoutAttributeLeading
                                                       relatedBy:NSLayoutRelationEqual
                                                          toItem:self
                                                       attribute:NSLayoutAttributeLeading
                                                      multiplier:1.0f
                                                        constant:0];
    trailingConstraint  = [NSLayoutConstraint constraintWithItem:view
                                                       attribute:NSLayoutAttributeTrailing
                                                       relatedBy:NSLayoutRelationEqual
                                                          toItem:self
                                                       attribute:NSLayoutAttributeTrailing
                                                      multiplier:1.0f
                                                        constant:0];
    topConstraint       = [NSLayoutConstraint constraintWithItem:view
                                                       attribute:NSLayoutAttributeTop
                                                       relatedBy:NSLayoutRelationEqual
                                                          toItem:self
                                                       attribute:NSLayoutAttributeTop
                                                      multiplier:1.0f
                                                        constant:0];
    
    bottomConstraint    = [NSLayoutConstraint constraintWithItem:view
                                                       attribute:NSLayoutAttributeBottom
                                                       relatedBy:NSLayoutRelationEqual
                                                          toItem:self
                                                       attribute:NSLayoutAttributeBottom
                                                      multiplier:1.0f
                                                        constant:0.0f];
    
    [self addConstraints:@[leadingConstraint, trailingConstraint, topConstraint, bottomConstraint]];
}

@end
